package com.lagou.edu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

/**
 * 封装Model
 * 测试模型,无入参
 */
@Controller
@RequestMapping("demo")
public class ModelController {

    /**
     * http://localhost:8080/demo/handle01
     */
    @RequestMapping("handle01")
    public ModelAndView handle01(){
        Date date = new Date();
        // 返回服务器时间到前端
        // 封装了数据和页面的
        ModelAndView modelAndView = new ModelAndView();
        // addObject 向请求域中 request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        // 视图 封装跳转的页面
        // 物理视图名 /WEB-INF/jsp/success.jsp
        // 逻辑视图名
        modelAndView.setViewName("success");
        System.out.println("modelAndView " + modelAndView.getClass());
        return modelAndView;
    }

    /**
     * ModelAndView & ModelMap & Model & Map
     * BindingAwareModelMap extends ExtendedModelMap
     *                              ExtendedModelMap extends ModelMap implements Model
     *                                                       ModelMap extends LinkedHashMap<String, Object>
     */

    @RequestMapping("handle02")
    public String handle02(ModelMap modelMap){
        Date date = new Date();
        modelMap.addAttribute("date", date);
        System.out.println("modelMap " + modelMap.getClass());
        return "success";
    }

    @RequestMapping("handle03")
    public String handle03(Model model){
        Date date = new Date();
        model.addAttribute("date", date);
        System.out.println("model " + model.getClass());
        return "success";
    }

    @RequestMapping("handle04")
    public String handle04(Map<String, Object> map){
        Date date = new Date();
        map.put("date", date);
        System.out.println("map " + map.getClass());
        return "success";
    }

}
